/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERNAL_SOURCE_HPP
#define XERNAL_SOURCE_HPP

#include <boost/weak_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "al.h"
#include "Xern/IMovementListener.hpp"
#include "Xern/ISoundSource.hpp"
#include "Xern/ISerializableResource.hpp"
#include "Xern/Json.hpp"
#include "SoundCache.hpp"

namespace XernAL
{
	using namespace Xern;

	class Source: public ISoundSource, public ISerializableResource,
		public IMovementListener,
		public boost::enable_shared_from_this<Source>
	{
	public:
		Source(boost::shared_ptr<SoundCache> soundCache);
		~Source();

		virtual ResourceFlags GetFlags() const;
		virtual void UpdateState(boost::shared_ptr<IDataFilter> stateData);

		virtual const uuid &GetId() const;
		virtual void SetId(const uuid &id);

		virtual const char *GetMimeType() const;
		virtual const char *GetFileExtension() const;

		virtual bool IsFileSerializable() const;
		virtual bool IsMemorySerializable() const;

		virtual boost::shared_ptr<IDataFilter> GetSerializedData() const;

		virtual SourceState GetState() const;
		virtual void SetState(SourceState state);

		virtual bool IsLooping() const;
		virtual void SetLooping(bool loop);

		virtual bool IsGlobalSource() const;
		virtual void SetGlobalSource(bool value);

		virtual boost::shared_ptr<ISoundSampler> GetSampler() const;
		virtual void SetSampler(boost::shared_ptr<ISoundSampler> sampler);

		virtual void OnMove(boost::shared_ptr<Entity> sender);

		void Frame(float delta);

		void LoadData(const JsonObject &data);

	private:
		void PrepareBuffers();
		void StreamStart();
		void StreamBuffer();
		bool ReadBuffer(ALuint buffer);

		uuid id;
		boost::shared_ptr<SoundCache> soundCache;
		boost::shared_ptr<ISoundSampler> sampler;
		boost::shared_ptr<SoundBuffer> sampleBuffer;

		bool looping;
		SourceState currentState;
		ALuint source;
		ALenum format;
		ALuint streamBuffers[2];
		size_t streamBufferSize;
		char *streamBuffer;
		bool streamFinish;
		bool globalSource;
	};
};

#endif //#define XERNAL_SOURCE_HPP
